﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Values.xaml" />
        <ResourceDictionary Source="TextBoxStyle.xaml" />
        <ResourceDictionary Source="ErrorTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="ComboBoxFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Stroke="Black" StrokeThickness="0" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ComboBoxEditableTextBox"
           BasedOn="{StaticResource {x:Type TextBox}}"
           TargetType="{x:Type TextBox}">

        <Setter Property="FocusVisualStyle" Value="{x:Null}" />

        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst" />

        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />

        <Setter Property="AllowDrop" Value="true" />

        <Setter Property="BorderThickness" Value="0" />

        <Setter Property="BorderBrush" Value="{x:Null}" />

        <Setter Property="Padding" Value="0" />

    </Style>

    <Style x:Key="ComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">

        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="{x:Null}" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Foreground" Value="{StaticResource View.Foreground.Normal}" />

        <Setter Property="Padding" Value="4 2" />

        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">

                    <Border Name="Border"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="{StaticResource Control.Border.CornerRadius}"
                            Padding="{TemplateBinding Padding}">

                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" />

                            <Rectangle Grid.Column="1" Margin="{TemplateBinding Padding}" />

                            <Path Grid.Column="2"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  Data="{StaticResource Arrow.Down.Metro}"
                                  Fill="{TemplateBinding Foreground}" />
                        </Grid>

                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <!--
            <Style.Triggers>

            <Trigger Property="IsEnabled"
            Value="False">
            <Setter Property="Background"
            Value="{StaticResource Button.Background.Disabled}" />
            <Setter Property="BorderBrush"
            Value="{StaticResource Button.BorderBrush.Disabled}" />
            <Setter Property="Foreground"
            Value="{StaticResource Button.Foreground.Disabled}" />
            </Trigger>

            </Style.Triggers>
        -->

    </Style>

    <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
        <Grid>
            <ToggleButton x:Name="tgb"
                          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                          Background="{TemplateBinding Background}"
                          BorderBrush="{TemplateBinding BorderBrush}"
                          BorderThickness="{TemplateBinding BorderThickness}"
                          ClickMode="Press"
                          ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                          ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                          Focusable="false"
                          Foreground="{TemplateBinding Foreground}"
                          IsChecked="{Binding IsDropDownOpen,
                                              Mode=TwoWay,
                                              RelativeSource={RelativeSource TemplatedParent}}"
                          Padding="{TemplateBinding Padding}"
                          Style="{StaticResource ComboBoxToggleButtonStyle}">
                <ToggleButton.Content>
                    <TextBox x:Name="PART_EditableTextBox"
                             IsReadOnly="{Binding IsReadOnly,
                                                  RelativeSource={RelativeSource TemplatedParent}}"
                             Style="{StaticResource ComboBoxEditableTextBox}"
                             Text="{TemplateBinding Text}" />
                </ToggleButton.Content>
            </ToggleButton>

            <Popup Name="PART_Popup"
                   AllowsTransparency="true"
                   Focusable="False"
                   IsOpen="{TemplateBinding IsDropDownOpen}"
                   Placement="Bottom"
                   PopupAnimation="Fade">

                <Border x:Name="DropDownBorder"
                        MinWidth="{TemplateBinding ActualWidth}"
                        MaxHeight="{TemplateBinding MaxDropDownHeight}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="{StaticResource Control.Border.CornerRadius}">
                    <!--
                        <Border.CornerRadius>
                        <CornerRadius BottomLeft="{StaticResource Control.Border.CornerRadius.Value}"
                        BottomRight="{StaticResource Control.Border.CornerRadius.Value}"
                        TopLeft="0"
                        TopRight="0" />
                        </Border.CornerRadius>
                    -->
                    <ScrollViewer Name="DropDownScrollViewer">
                        <ItemsPresenter Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" />
                    </ScrollViewer>
                </Border>

            </Popup>

        </Grid>

        <ControlTemplate.Triggers>

            <Trigger SourceName="PART_EditableTextBox" Property="IsFocused" Value="true">
                <Setter Property="IsDropDownOpen" Value="true" />
            </Trigger>

        </ControlTemplate.Triggers>

    </ControlTemplate>

    <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">

        <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}" />

        <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}" />

        <Setter Property="Background" Value="{StaticResource MenuItemBackgroundBrush}" />
        <Setter Property="Foreground" Value="{StaticResource MenuItemForegroundBrush}" />
        <Setter Property="BorderBrush" Value="{StaticResource Control.BorderBrush.Normal}" />

        <Setter Property="BorderThickness" Value="{StaticResource View.Border.Thickness}" />

        <Setter Property="Padding" Value="4 2" />
        <!--
            <Setter Property="Padding">
            <Setter.Value>
            <Thickness Left="{StaticResource View.Region.Spacing.SmallValue}"
            Top="{StaticResource View.Region.Spacing.SmallValue}"
            Right="{StaticResource View.Region.Spacing.SmallValue}"
            Bottom="{StaticResource View.Region.Spacing.SmallValue}" />
            </Setter.Value>
            </Setter>
        -->

        <Setter Property="StaysOpenOnEdit" Value="True" />

        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />

        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
        <Setter Property="ScrollViewer.PanningMode" Value="Both" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">

                    <Grid>
                        <ToggleButton x:Name="tgb"
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                      Background="{TemplateBinding Background}"
                                      BorderBrush="{TemplateBinding BorderBrush}"
                                      BorderThickness="{TemplateBinding BorderThickness}"
                                      ClickMode="Press"
                                      Content="{TemplateBinding SelectionBoxItem}"
                                      ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                                      ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                      ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                      Focusable="false"
                                      Foreground="{TemplateBinding Foreground}"
                                      IsChecked="{Binding IsDropDownOpen,
                                                          Mode=TwoWay,
                                                          RelativeSource={RelativeSource TemplatedParent}}"
                                      Padding="{TemplateBinding Padding}"
                                      Style="{StaticResource ComboBoxToggleButtonStyle}" />

                        <Popup Name="PART_Popup"
                               AllowsTransparency="true"
                               Focusable="False"
                               IsOpen="{TemplateBinding IsDropDownOpen}"
                               Placement="Bottom"
                               PopupAnimation="Fade">

                            <Border x:Name="DropDownBorder"
                                    MinWidth="{TemplateBinding ActualWidth}"
                                    MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                    Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    CornerRadius="{StaticResource Control.Border.CornerRadius}">
                                <!--
                                    <Border.CornerRadius>
                                    <CornerRadius BottomLeft="{StaticResource Control.Border.CornerRadius.Value}"
                                    BottomRight="{StaticResource Control.Border.CornerRadius.Value}"
                                    TopLeft="0"
                                    TopRight="0" />
                                    </Border.CornerRadius>
                                -->
                                <ScrollViewer Name="DropDownScrollViewer">
                                    <ItemsPresenter Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Border>

                        </Popup>

                    </Grid>

                    <ControlTemplate.Triggers>

                        <!--
                            <Trigger Property="IsDropDownOpen" Value="true">
                            <Setter TargetName="DropDownBorder" Property="CornerRadius">
                            <Setter.Value>
                            <CornerRadius BottomLeft="0"
                            BottomRight="0"
                            TopLeft="{StaticResource Control.Border.CornerRadius.Value}"
                            TopRight="{StaticResource Control.Border.CornerRadius.Value}" />
                            </Setter.Value>
                            </Setter>
                            </Trigger>
                        -->

                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>

            <Trigger Property="IsEditable" Value="true">
                <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}" />
                <!--
                    <Setter Property="Padding"
                    Value="1 2 4 2" />
                -->
            </Trigger>

            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="{StaticResource MenuItemDisabledBackgroundBrush}" />
                <Setter Property="BorderBrush" Value="{StaticResource MenuItemDisabledBorderBrush}" />
                <Setter Property="Foreground" Value="{StaticResource MenuItemDisabledForegroundBrush}" />
            </Trigger>

            <!--
                <Trigger Property="IsFocused"
                Value="true">
                <Setter Property="IsDropDownOpen"
                Value="True" />
                </Trigger>
            -->

            <Trigger Property="HasItems" Value="False">
                <Setter Property="IsEnabled" Value="False" />
            </Trigger>

        </Style.Triggers>

    </Style>

</ResourceDictionary>